package com.tanhua.controller;


import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo2;
import com.tanhua.server.impl.UserInfoService;
import com.tanhua.server.impl.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private Userservice userService;
    //取消喜欢
    @DeleteMapping("/like/{uid}")
    public ResponseEntity delectLike(@PathVariable("uid") Long uid){
        userService.delectLike(uid);
        return ResponseEntity.ok(null);
    }

    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    @GetMapping("/friends/{type}")
    public ResponseEntity findPage(@PathVariable("type") Integer type, @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String nickname) {
        PageResult pr = userService.findBytype(type, page, pagesize, nickname);

        return ResponseEntity.ok(pr);
    }


    //查看互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("/counts")
    public ResponseEntity getcounts() {
        Long userId = UserHolder.getUserId();
        Map map = userService.getcounts(userId);
        return ResponseEntity.ok(map);
    }

    /*更换电话号码（3）
    保存电话号
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.savePhone(phone);

        return ResponseEntity.ok("修改成功");
    }

    /*更换电话号码（1）
    发送验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //1. 获取手机号
        String phone = UserHolder.getMobile();
        //2. 发送验证码
        userService.sendCode(phone);
        //3. 返回数据
        return ResponseEntity.ok("验证码发送成功");
    }

    /*更换电话号码（2）
   校验验证码
    */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map mapp) {
        String verificationCode = (String) mapp.get("verificationCode");
        Map map = userService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(map);
    }

    /**
     * 换头像
     *
     * @return
     *///users/header
    @PostMapping("/header")
    public ResponseEntity Header(MultipartFile headPhoto) {
        try {
            userInfoService.updateHead(headPhoto, UserHolder.getUserId());
        } catch (IOException e) {
            throw new BusinessException(ErrorResult.faceError());
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID) {
        //判断是否传回用户id 如果为null 给予赋值
        Long id = UserHolder.getUserId();
        if (userID == null) {
            userID = Long.valueOf(id);
        }

        UserInfoVo2 userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {

        //2、获取id
        Long id = UserHolder.get().getId();

        //设置id
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}